package com.chuangshi.core.vote.member.service.impl;

import cn.hutool.core.util.StrUtil;
import com.chuangshi.core.kit.Column;
import com.chuangshi.core.kit.IdWorker;
import com.chuangshi.core.kit.Result;
import com.chuangshi.core.mongodb.MongodbServiceImpl;
import com.chuangshi.core.vote.member.controller.admin.request.MemberAdminV1ListRequest;
import com.chuangshi.core.vote.member.entity.Member;
import com.chuangshi.core.vote.member.entity.column.MemberColumn;
import com.chuangshi.core.vote.member.service.MemberService;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员业务接口实现
 *
 * @author marcus
 * @date 2018-11-20
 */
@Service
public class MemberServiceImpl extends MongodbServiceImpl<Member> implements MemberService {

    @Override
    public Result listForAdmin(MemberAdminV1ListRequest request) {
        Criteria criteria = Criteria.where(Column.APP_ID).is(request.getAppId())
                        .and(Column.SYSTEM_STATUS).is(true);
        if (StrUtil.isNotBlank(request.getMemberId())) {
            criteria.and(MemberColumn.MEMBER_ID).regex(".*?" + request.getMemberId() + ".*");
        }
        if (StrUtil.isNotBlank(request.getMemberMobile())) {
            criteria.and(MemberColumn.MEMBER_MOBILE).regex(".*?" + request.getMemberMobile() + ".*");
        }
        if (StrUtil.isNotBlank(request.getMemberNickName())) {
            criteria.and(MemberColumn.MEMBER_NICK_NAME).regex(".*?" + request.getMemberNickName() + ".*");
        }

        List<Sort.Order> orders = new ArrayList<Sort.Order>();
        orders.add(new Sort.Order(Sort.Direction.DESC, MemberColumn.MEMBER_ID));
        Sort sort = Sort.by(orders);

        Integer total = count(criteria);
        List<Member> list = list(criteria, sort, request.getPageIndex(), request.getPageSize());


        Result result = new Result();
        result.setTotal(total);
        result.setList(list);

        return result;
    }


    @Override
    public Member findByUnionIdAndMiniOpenId(String appId, String memberWechatUnionId, String memberWechatMiniOpenId) {
        Criteria criteria = Criteria.where(Column.APP_ID).is(appId)
                .orOperator(Criteria.where(MemberColumn.MEMBER_WECHAT_UNION_ID).is(memberWechatUnionId), Criteria.where(MemberColumn.MEMBER_WECHAT_MINI_OPEN_ID).is(memberWechatMiniOpenId))
                .and(Column.SYSTEM_STATUS).is(true);
        return find(criteria);
    }

    @Override
    public Member findByMobile(String appId, String memberMobile) {
        Criteria criteria = Criteria.where(Column.APP_ID).is(appId)
                .and(MemberColumn.MEMBER_MOBILE).is(memberMobile)
                .and(Column.SYSTEM_STATUS).is(true);
        return find(criteria);
    }

    @Override
    public Member findByOpenId(String openId) {
        Criteria criteria = Criteria.where(MemberColumn.MEMBER_WECHAT_MP_OPEN_ID).is(openId)
                .and(Column.SYSTEM_STATUS).is(true);
        return find(criteria);
    }
}